#include <bits/stdc++.h>
using namespace std;
class Sign{
	public:
		string id;
		int in_h;
		int in_m;
		int in_s;
		int out_h;
		int out_m;
		int out_s;
};
int main()
{
	int n, i, earliest = 0, latest = 0;
	cin >> n;
	Sign *s = new Sign[n];
	for(i = 0; i < n; i++){
		cin >> s[i].id;
		scanf("%d:%d:%d %d:%d:%d", &s[i].in_h, &s[i].in_m, &s[i].in_s, 
				&s[i].out_h, &s[i].out_m, &s[i].out_s);
	}
	for(i = 1; i < n; i++)
	{
		if(s[earliest].in_h > s[i].in_h){
			earliest = i;
		}else if(s[earliest].in_h < s[i].in_h){
			continue;
		}else{
			if(s[earliest].in_m > s[i].in_m){
				earliest = i;
			}else if(s[earliest].in_m < s[i].in_m){
				continue;
			}else{
				if(s[earliest].in_s > s[i].in_s){
					earliest = i;
				}else if(s[earliest].in_s < s[i].in_s){
					continue;
				}
			}
		}
	}
	for(i = 1; i < n; i++){
		if(s[latest].out_h < s[i].out_h){
			latest = i;
		}else if(s[latest].out_h > s[i].out_h){
			continue;
		}else{
			if(s[latest].out_m < s[i].out_m){
				latest = i;
			}else if(s[latest].out_m > s[i].out_m){
				continue;
			}else{
				if(s[latest].out_s < s[i].out_s){
					latest = i;
				}else if(s[latest].out_s > s[i].out_s){
					continue;
				}
			}
		}
	}
	cout << s[earliest].id << " " << s[latest].id;
	return 0;
} 
